package com.yue.rpc.remoting.transport.netty.client;

import com.yue.rpc.enums.CompressTypeEnum;
import com.yue.rpc.enums.SerializationTypeEnum;
import com.yue.rpc.extension.ExtensionLoader;
import com.yue.rpc.factory.SingletonFactory;
import com.yue.rpc.registry.ServiceDiscovery;
import com.yue.rpc.remoting.constants.RpcConstants;
import com.yue.rpc.remoting.dto.RpcMessage;
import com.yue.rpc.remoting.dto.RpcRequest;
import com.yue.rpc.remoting.dto.RpcResponse;
import com.yue.rpc.remoting.transport.RpcRequestTransport;
import com.yue.rpc.remoting.transport.netty.codec.RpcMessageDecoder;
import com.yue.rpc.remoting.transport.netty.codec.RpcMessageEncoder;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.IdleStateHandler;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author zhangqiyue
 * @Description
 * @Date 2023/3/7 15:24
 */
public class NettyClient {
    public static String HOST = "127.0.0.1";
    public static int PORT = 9999;

    public static Channel channel = null;
    private static AtomicInteger id = new AtomicInteger(1);

    private static ConcurrentHashMap<String, RpcResponse> result = new ConcurrentHashMap<String, RpcResponse>();

    public static void connect(final NettyClientListener listener) {
        try {
            EventLoopGroup group = new NioEventLoopGroup();
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(group).channel(NioSocketChannel.class);
            bootstrap.handler(new ChannelInitializer<Channel>() {
                @Override
                protected void initChannel(Channel ch) throws Exception {
                    ChannelPipeline pipeline = ch.pipeline();
                    pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
                    pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));
                    pipeline.addLast("decoder", new ObjectDecoder(Integer.MAX_VALUE, ClassResolvers.cacheDisabled(null)));
                    pipeline.addLast("encoder", new ObjectEncoder());
                    pipeline.addLast("handler", new NettyClientHandler(listener));
                }
            });

            bootstrap.option(ChannelOption.SO_KEEPALIVE, true);

            channel = bootstrap.connect(HOST, PORT).sync().channel();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}